package com.itcast.demoone.kotlin

fun main() {
    lambdaDemo1()
    lambdaDemo2()
}


fun lambdaDemo1() {
    val numbers = listOf(1, 2, 3, 4, 5, 6)

    // 1. 过滤 - 找出偶数
    val evenNumbers = numbers.filter { it % 2 == 0 }
    println(evenNumbers)

    // 2. 映射 - 乘以2
    val doubledNumbers = numbers.map { it * 2 }
    println(doubledNumbers)

    // 3. 查找 - 找到第一个偶数
    val firstEvenNumber = numbers.find { it % 2 == 0 }

    // 4. 查找 - 第一个大于4的数
    val firstGreaterThan4 = numbers.first { it > 4 }
}

fun lambdaDemo2() {
    fun calculate(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
        return operation(num1, num2)
    }

    val res1 = calculate(10, 5){ a, b -> a + b }
    val res2 = calculate(10, 5){ a, b -> a - b }
    val res3 = calculate(10, 5){ a, b -> a * b }
    val res4 = calculate(10, 5){ a, b -> a / b }
    println("res1: $res1, res2: $res2, res3: $res3, res4: $res4")
}